package com.jmu.edu.IMApplication.adapter;

import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.bean.Emoji;
import com.jmu.edu.IMApplication.event.NotifyToSendMessageEvent;
import com.jmu.edu.IMApplication.event.SendMessageEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

public class EmojiItemAdapter extends RecyclerView.Adapter<EmojiItemAdapter.MyViewHolder> {
    private ArrayList<Emoji> emojiList;

    public void setEmojiList(ArrayList<Emoji> emojiList) {
        this.emojiList = emojiList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_emoji, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Emoji emoji = emojiList.get(position);
        StringBuilder stringBuilder = new StringBuilder(String.valueOf(Character.toChars(emoji.getUnicode())));
        //stringBuilder.append(emoji.getName());
        holder.emojiText.setText(stringBuilder.toString());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建发送消息事件并发送，这个事件将由ChatFragment接收
                SendMessageEvent event = new SendMessageEvent();
                event.setMessageType(NotifyToSendMessageEvent.TEXT_TYPE);
                event.setMessageContent(holder.emojiText.getText().toString());
                EventBus.getDefault().post(event);
            }
        });
    }

    @Override
    public int getItemCount() {
        return emojiList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView emojiText;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            emojiText = itemView.findViewById(R.id.item_emoji_text);
        }
    }
}
